﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;

namespace CacheManager
{
    /// <summary>
    /// MemoryCache管理工具类
    /// </summary>
    public class MemoryCacheManager
    {
        private static MemoryCacheManager _memoryCacheManager;
        private static readonly object _lockObj = new object();
        private static Dictionary<string, MemoryCache> _cacheManager = new Dictionary<string, MemoryCache>();

        private MemoryCacheManager()
        {

        }

        /// <summary>
        /// 获取一个MemoryCacheManager单例实例
        /// </summary>
        /// <returns></returns>
        public static MemoryCacheManager GetMemoryCacheManager()
        {
            if(_memoryCacheManager == null)
            {
                lock (_lockObj)
                {
                    if(_memoryCacheManager == null)
                    {
                        return _memoryCacheManager = new MemoryCacheManager();
                    }
                }
            }
            return _memoryCacheManager;
        }

        /// <summary>
        /// 将对象存入缓存管理器，如果重复覆盖之前的操作
        /// </summary>
        /// <param name="memoryCache"></param>
        public void Add(string cacheName)
        {
            lock (_lockObj)
            {
                if (_cacheManager.ContainsKey(cacheName))
                {
                    _cacheManager.Remove(cacheName);
                }
                _cacheManager.Add(cacheName, new MemoryCache(cacheName));
            }
        }

        /// <summary>
        /// 获取对应的MemoryCache
        /// </summary>
        /// <param name="cacheName">对应缓存的名称</param>
        /// <returns></returns>
        public MemoryCache Get(string cacheName)
        {
            if (_cacheManager.ContainsKey(cacheName))
            {
                return _cacheManager[cacheName];
            }
            Add(cacheName);
            return _cacheManager[cacheName];
        }

        /// <summary>
        /// 判断是否存在对应的MemoryCache对象
        /// </summary>
        /// <param name="cacheName"></param>
        /// <returns></returns>
        public bool IsContainCache(string cacheName)
        {
            return _cacheManager.ContainsKey(cacheName);
        }

        /// <summary>
        /// 通过MemoryCache的名称查找对应的MemoryCache对象
        /// </summary>
        /// <param name="cacheName"></param>
        /// <returns></returns>
        public MemoryCache this[string cacheName]
        {
            get
            {
                if (_cacheManager.ContainsKey(cacheName))
                {
                    return _cacheManager[cacheName]; 
                }
                return null;
            }
            set
            {
                _cacheManager.Add(cacheName, value);
            }
        }

       
        /// <summary>
        /// 移除某个MemoryCache缓存项
        /// </summary>
        /// <param name="cacheName"></param>
        public void Remove(string cacheName)
        {
            if (_cacheManager.ContainsKey(cacheName))
            {
                _cacheManager.Remove(cacheName);
            }
        }
    }
}
